﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Scenes.ProcessScene.TopMembers;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.ScrollLayout;
using Spinning.Spinning.Interface;

namespace Spinning.Spinning.Scenes.ProcessScene.Store
{
    class StoreItemsScene : BaseScene, ISaveLoad
    {
        List<ScrollLayoutElementItemForStore> totalPriceList = new List<ScrollLayoutElementItemForStore>();
        public StoreItemsScene()
        {
            Size = new Vector2(426, 575);
        }

        public override void OnLoad()
        {
            base.OnLoad();

            ScrollMenu scs = new ScrollMenu();//不用改
            scs.Position = new Vector2(11, 30);
            scs.Color = Color.Transparent;
            scs.Size = new Vector2(405, 286 + 138);
            scs.OnLoad();
            Attach(scs);

            //參考用////
            ScrollLayoutElementItemForStore ses = new ScrollLayoutElementItemForStore(this);
            ses.OnLoad();
            ses.Quantity = 0;
            ses.SpinningPrice = 100;
            scs.AddItem(ses);
            totalPriceList.Add(ses);

            ses = new ScrollLayoutElementItemForStore(this);
            ses.OnLoad();
            ses.ItemType = Constants.ItemTypes.Sandbag;
            ses.SpinningPrice = 200;
            ses.State_Type = ScrollLayoutElementItemForStore.StateType.Focus;
            ses.Quantity = 0;
            scs.AddItem(ses);
            totalPriceList.Add(ses);

            ses = new ScrollLayoutElementItemForStore(this);
            ses.OnLoad();
            ses.ItemType = Constants.ItemTypes.Copter;
            ses.SpinningPrice = 300;
            ses.State_Type = ScrollLayoutElementItemForStore.StateType.Normal;
            ses.Quantity = 0;
            scs.AddItem(ses);
            totalPriceList.Add(ses);

            ses = new ScrollLayoutElementItemForStore(this);
            ses.OnLoad();
            ses.ItemType = Constants.ItemTypes.Windmill;
            ses.SpinningPrice = 400;
            ses.State_Type = ScrollLayoutElementItemForStore.StateType.Normal;
            ses.Quantity = 0;
            scs.AddItem(ses);
            totalPriceList.Add(ses);

            ses = new ScrollLayoutElementItemForStore(this);
            ses.OnLoad();
            ses.ItemType = Constants.ItemTypes.Doll;
            ses.SpinningPrice = 500;
            ses.State_Type = ScrollLayoutElementItemForStore.StateType.Normal;
            ses.Quantity = 0;
            scs.AddItem(ses);
            totalPriceList.Add(ses);

            /////////

            SortChildren();
        }

        public int TotalPrice
        {
            get
            {
                int t = 0;
                foreach (ScrollLayoutElementItemForStore it in totalPriceList)
                {
                    //if (it.State_Type == ScrollLayoutElementItemForStore.StateType.Focus)
                        t += it.SpinningPrice * it.BuyQuantity;
                }
                return t;
            }
        }

        public override BaseScene Clone()
        {
            throw new NotImplementedException();
        }

        public void Save(Unitity.Storage.SaveData state)
        {
            state.BuyedItems.Clear();

            foreach (ScrollLayoutElementItemForStore it in totalPriceList)
            {
                it.Quantity = it.Quantity + it.BuyQuantity;
                it.BuyQuantity = 0;
                state.BuyedItems.Add(it.Quantity);
            }
        }

        public void Load(Unitity.Storage.SaveData state)
        {
            for (int i = 0; i < totalPriceList.Count; i++)
            {
                ScrollLayoutElementItemForStore it = totalPriceList[i];
                if(i<state.BuyedItems.Count)
                it.Quantity = state.BuyedItems[i];
            }
        }
    }
}
